﻿package ua.mihaylenko.canvas.gui.photoshop.scrollbar
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class GUIScrollBar extends Sprite
	{
		public var _smcThumb:Sprite;
		public var _smcBackground:Sprite;
		private var _nMax:int;
		private var _nMin:int;
		private static var _nsMaxScroll:int;
		private var _iScrollPosition:int;
		public function GUIScrollBar() 
		{
			_nMax = this.width - _smcThumb.width;
			_nMin = 0;
			_smcThumb.addEventListener(MouseEvent.MOUSE_DOWN, startDragThumb);
			_smcBackground.addEventListener(MouseEvent.CLICK, setThumbPosition);
			if (!_nsMaxScroll) {
				_nsMaxScroll = (this.width - _smcThumb.width);
			}
		}
		private function startDragThumb(event:MouseEvent) {
			_smcThumb.addEventListener(MouseEvent.MOUSE_UP, stopDragThumb);
			//_smcThumb.addEventListener(MouseEvent.MOUSE_OUT, stopDragThumb);
			_smcThumb.addEventListener(MouseEvent.MOUSE_MOVE, onDragThumb);
			_smcThumb.startDrag(false, new Rectangle(0, _smcThumb.y, (this.width - _smcThumb.width)));
		}
		private function setThumbPosition(event:MouseEvent) {
			var nPostion:Number = this.mouseX - _smcThumb.width / 2;
			
			if (nPostion < 0) {
				_smcThumb.x = 0;
			}else {
				if (nPostion > (this.width - _smcThumb.width)) {
					_smcThumb.x = (this.width - _smcThumb.width);
				}else {
					_smcThumb.x = nPostion;
				}
			}
			updateScrollPosition();
		}
		private function onDragThumb(event:MouseEvent) {
			updateScrollPosition();
		}
		private function updateScrollPosition() {
			var nPercentPosition:Number = _smcThumb.x / _nsMaxScroll;
			var nScrollPosition:Number = (Math.abs(_nMax) + Math.abs(_nMin)) * nPercentPosition + _nMin+0.5;
			_iScrollPosition = int(nScrollPosition);
			this.dispatchEvent(new ScrollBarEvent(ScrollBarEvent.CHANGE_SCROLL_POSITION));
		}
		public function set scrollPosition(nValue:Number) {
			_iScrollPosition = int(nValue);
			var nPercentPosition:Number = (nValue - _nMin) / (Math.abs(_nMax) + Math.abs(_nMin))
			_smcThumb.x = nPercentPosition * _nsMaxScroll;
			updateScrollPosition();
		}
		public function get scrollPosition():Number {
			return _iScrollPosition;
		}
		public function stopDragThumb(event:MouseEvent) {
			_smcThumb.stopDrag();
			_smcThumb.removeEventListener(MouseEvent.MOUSE_UP, stopDragThumb);
			_smcThumb.removeEventListener(MouseEvent.MOUSE_OUT, stopDragThumb);
			_smcThumb.removeEventListener(MouseEvent.MOUSE_MOVE, onDragThumb);
			updateScrollPosition();
		}
		public function stopDragThumb1(event:MouseEvent) {
			_smcThumb.stopDrag();
			_smcThumb.removeEventListener(MouseEvent.MOUSE_UP, stopDragThumb);
			_smcThumb.removeEventListener(MouseEvent.MOUSE_OUT, stopDragThumb);
			_smcThumb.removeEventListener(MouseEvent.MOUSE_MOVE, onDragThumb);
		}
		public function set maxPosition(nValue:Number) {
			_nMax = nValue;
		}
		public function get maxPosition():Number {
			return _nMax;
		}
		public function set minPosition(nValue:Number) {
			_nMin = nValue;
		}
		public function get minPosition():Number {
			return _nMin;
		}
		
	}
	
}